﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Calculater
{
    /**
     * C#口算训练程序
     */
    public partial class Form1 : Form
    {
        int sec = 0;  // 计时数字
        int score = 0; // 分数
        int first;  // 第一个数字
        int second;  // 第二个数字
        int speed = 3000; // 自动显示间隔
        bool isOn = false;  //开始标记
        int mode = 1; // 题目类型编号，1代表10以内加减法，2代表20以内加减法，3代表10以内乘法


        public Form1()
        {
            InitializeComponent();
        }


        /**
         * 开始按钮被点击
         */
        private void btn_next_Click(object sender, EventArgs e)
        {
            text_result.Text = null;
            text_result.Enabled = true;
            text_result.Focus();
            lab_time.Enabled = true;
            // 题目计时开始
            timer1.Start();

            // 自动显示下一题计时开始
            if (checkBox1.Checked)
            {
                timer2.Start();
            }

            // 开始后复选框禁用
            checkBox1.Enabled = false;
            checkBox2.Enabled = false;
            if (isOn)
            {
                start();
            }
            else
            {
                isOn = true;
                btn_next.Text = "下一题";
                btn_next.Width = 50;
                btn_stop.Visible = true;
                return;
            }

        }

        /**
         * 结束按钮被点击
         */
        private void btn_stop_Click(object sender, EventArgs e)
        {
            isOn = false;
            text_result.Enabled = false;
            btn_next.Width = 112;
            lab_score.Text = "0";
            btn_next.Text = "开始";
            btn_stop.Visible = false;
            checkBox1.Enabled = true;
            checkBox2.Enabled = true;

            // 秒数归零，停止计时
            lab_time.Enabled = false;
            lab_time.Text = "0s";
            timer1.Stop();
            timer2.Stop();

            MessageBox.Show("本次得分：" + score.ToString() + "\n" + "用时" + sec + "秒");
            score = 0;
            sec = 0;
        }

        /**
         * 刷新题目
         */
        private void start()
        {
            switch (mode)
            {
                case 1:
                    Mode_Ten();
                    break;
                case 2:
                    Mode_Twen();
                    break;
                case 3:
                    Mode_Modi();
                    break;
            }
        }

        /**
         * 绑定快捷键Enter进行下一题
         */
        private void text_result_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {

                if (CheckResult())
                {
                    img_isR.Image = Properties.Resources.对号2;
                    score++;
                    lab_score.Text = score.ToString();
                }
                else img_isR.Image = Properties.Resources.错误_wrong;
                text_result.Text = null;
                start();

            }
        }

        /**
         * 10以内乘法
         */
        private void Mode_Modi()
        {
            //设置随机数
            Random rd1 = new Random();
            //生成1-10的随机数
            num1.Text = rd1.Next(1, 10).ToString();
            num2.Text = rd1.Next(1, 10).ToString();
            lab_mode.Text = "×";
            first = int.Parse(num1.Text);
            second = int.Parse(num2.Text);
        }



        /**
         * 10以内加减法
         */
        private void Mode_Ten()
        {
            //设置随机数
            Random rd1 = new Random();

            //生成1-10的随机数
            num1.Text = rd1.Next(1, 10).ToString();
            num2.Text = rd1.Next(1, 10).ToString();

            first = int.Parse(num1.Text);
            second = int.Parse(num2.Text);
            //随机加减法
            if (rd1.Next(1, 10) > 5)
            {
                lab_mode.Text = "+";
            }
            else
            {
                lab_mode.Text = "-";
                second *= -1;
            }

        }


        /**
         * 20以内加减法
         */
        private void Mode_Twen()
        {
            //设置随机数
            Random rd1 = new Random();

            //生成1-20的随机数
            num1.Text = rd1.Next(1, 20).ToString();
            num2.Text = rd1.Next(1, 20).ToString();


            first = int.Parse(num1.Text);
            second = int.Parse(num2.Text);
            //随机加减法
            if (rd1.Next(1, 10) > 5)
            {
                lab_mode.Text = "+";
            }
            else
            {
                lab_mode.Text = "-";
                second *= -1;
            }
        }



        /**
         * 题目检查事件
         */
        private bool CheckResult()
        {
            int result = 0;
            try
            {
                result = int.Parse(text_result.Text);
            }
            catch (Exception e)
            {
                MessageBox.Show("答案输入有误！！");
            }
            switch (mode)
            {
                case 1:
                    if (checkBox2.Checked) lab_as1.Text = first.ToString() + lab_mode.Text + num2.Text + "=" + result.ToString();
                    if (result == first + second)
                    {
                        return true;
                    }
                    break;
                case 2:
                    if (checkBox2.Checked) lab_as1.Text = first.ToString() + lab_mode.Text + num2.Text + "=" + result.ToString();
                    if (result == first + second)
                    {
                        return true;
                    }
                    break;
                case 3:
                    if (checkBox2.Checked) lab_as1.Text = first.ToString() + lab_mode.Text + num2.Text + "=" + result.ToString();
                    if (result == first * second)
                    {
                        return true;
                    }
                    break;
            }
            return false;
        }



        /**
         * 三种题目类型变更事件
         */
        private void rbtn_modeTen_CheckedChanged(object sender, EventArgs e)
        {
            mode = 1;
            start();
        }

        private void rbtn_modeTwen_CheckedChanged(object sender, EventArgs e)
        {
            mode = 2;
            start();
        }
        private void rbtn_modeModi_CheckedChanged(object sender, EventArgs e)
        {

            mode = 3;
            start();
        }

        /**
         * 自动显示下一题选中后按钮的禁用与启用
         */
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

            if (checkBox1.Checked)
            {
                btn_fast.Enabled = true;
                btn_slow.Enabled = true;

            }
            else
            {
                btn_fast.Enabled = false;
                btn_slow.Enabled = false;
            }
        }

        /**
         * 自动显示答案选中后按钮的禁用与启用
         */
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                lab_as0.Enabled = true;
                lab_as1.Enabled = true;
            }
            else
            {
                lab_as0.Enabled = false;
                lab_as1.Enabled = false;
            }
        }


        /**
         * 用时计时器每秒事件
         */
        private void timer1_Tick(object sender, EventArgs e)
        {
            sec++;
            lab_time.Text = sec + "s";
        }

        /**
         * 自动显示题目每秒事件
         */
        private void timer2_Tick(object sender, EventArgs e)
        {
            start();
        }


        /**
         * 快一些
         */
        private void btn_fast_Click(object sender, EventArgs e)
        {
            if (speed - 1000 == 0)
            {
                btn_fast.Enabled = false;
                return;
            }

            speed -= 1000;
            timer2.Interval = speed;
            btn_slow.Enabled = true;
        }

        /**
         * 慢一些
         */
        private void btn_slow_Click(object sender, EventArgs e)
        {

            if (speed + 1000 == 5000)
            {
                btn_slow.Enabled = false;
                return;
            }
            speed += 1000;
            timer2.Interval = speed;
            btn_fast.Enabled = true;

        }
    }
}

